#ifndef MODBUSPOLLER_H
#define MODBUSPOLLER_H

#include "receive_processor.h"
#include "historypage.h"
#include "devicemonitor.h"

#include <QObject>
#include <QDebug>
#include <QTimer>
#include <QModbusRtuSerialMaster>
#include <QModbusDataUnit>
#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QVariant>
#include <QTime>
#include <QQueue>


class ModbusPoller : public QObject {
    Q_OBJECT
public:
    ModbusPoller(QObject *parent = nullptr);
    ///开始访问寄存器
    void stopPolling();
    ///停止访问寄存器
    void startPolling();
    ///连接设备
    bool connect_device();
    ///断开与设备的连接
    void disconnect_device();


    ///配置MODBUS RTU串口相关信息
    void SetUpModbusRTU(
        const QVariant &Port,
        const QVariant &BaudRate,
        const QVariant &Parity,
        const QVariant &DataBits,
        const QVariant &StopBits,
        int Time_out
        );


    bool connected;
    QTimer pollTimer;
    QList<Receive_Processor*> *receive_processor_list;
    HistoryPage *history_page;
    DeviceMonitor *device_monitor;
    QList<int> activeDevices;
private:
    //
    QModbusRtuSerialMaster *modbusMaster;
    QQueue<int> addressQueue;


private slots:
    void pollData();

    void processModbusResponse(const QModbusDataUnit &unit);
};

#endif // MODBUSPOLLER_H
